import * as api from '../api';
import axios from 'axios'
let generator = {}
// 查询生成表数据
generator.listTable = function (params) {
  return api.request('GET', '/mysql/list?order=asc&limit=10&offset=0', params);
}
// 查询db数据库列表
generator.listDbTable = function (params) {
  return api.request('GET', '/tool/gen/db/list', params);
}
// 查询表详细信息
generator.getGenTable = function (params) {
  return api.request('GET', '/tool/gen/', params);
}
// 修改代码生成信息
generator.updateGenTable = function (params) {
  return api.request('GET', '/tool/gen', params);
}
// 导入表
generator.importTable = function (params) {
  return api.request('GET', '/tool/gen/importTable', params);
}
// 预览生成代码
generator.previewTable = function (params) {
  return api.request('GET', '/tool/gen/preview/' + params.tableId);
}
// 下载表
generator.downloadFile = function (params) {
  return api.request('GET', '/tool/gen/genCode/' + params.tableId);
}
// 删除表数据
generator.delTable = function (params) {
  return api.request('delete', '/tool/gen/' + params.tableId);
}
generator.downLoadZip=function (str, filename) {
  let url = baseUrl + str
  axios({
    method: 'get',
    url: url,
    responseType: 'blob',
    headers: { 'Authorization': 'Bearer ' + getToken() }
  }).then(res => {
    generator.resolveBlob(res, mimeMap.zip)
  })
}
const mimeMap = {
  xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  zip: 'application/zip'
}
/**
 * 解析blob响应内容并下载
 * @param {*} res blob响应内容
 * @param {String} mimeType MIME类型
 */
generator.resolveBlob=function (res, mimeType) {
  const aLink = document.createElement('a')
  let blob = new Blob([res.data], { type: mimeType })
  let patten = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
  let contentDisposition = decodeURI(res.headers['content-disposition'])
  let result = patten.exec(contentDisposition)
  let fileName;
  if(result){
    fileName = result[1];
  }else{
    fileName = "inspur.zip";
  }
  fileName = fileName.replace(/\"/g, '')
  aLink.href = URL.createObjectURL(blob)
  aLink.setAttribute('download', fileName) // 设置下载文件名称
  document.body.appendChild(aLink)
  aLink.click()
  document.body.appendChild(aLink)
}
export default generator


